package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.RepairPaymentDao;
import cn.gson.crm.model.mapper.RepairPaymentMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class RepairPaymentService {
    @Autowired
    RepairPaymentMapper repairPaymentMapper;
    @Autowired
    RepairPaymentDao repairPaymentDao;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //查询当前登录人所经手的维修回款
    public Map<String,Object> selectpaymen(HttpSession session,Integer rid,Integer page,Integer size){
        rid=fouyuan(session);
        List<RepairPayment> repairPayments=repairPaymentMapper.selectpaymen(rid);
        //查询当前人下所有客户
        User login = (User)session.getAttribute("login");
        int ygId= (int) login.getEmployees().get(0).getYgId();
        List<Customer> customerList=repairPaymentMapper.selectcustomer(ygId);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<RepairPayment> repairPayments2=repairPaymentMapper.selectpaymen(rid);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",repairPayments2);
        map.put("tong",repairPayments);
        map.put("customerList",customerList);
        return map;
    };
    //多条件查询维修回款
    public Map<String,Object> selectwhere( HttpSession session,Integer rid,  String where,Integer page,Integer size){
        rid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<RepairPayment> repairPayments2=repairPaymentMapper.selectwhere(rid,where);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",repairPayments2);
        return map;
    };
    //时间查询维修回款
    public  Map<String,Object> selecttime(HttpSession session,Integer rid, String statr,String endtime,Integer page,Integer size){
        rid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<RepairPayment> repairPayments2=repairPaymentMapper.selecttime(rid,statr,endtime);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",repairPayments2);
        return map;

    };
    //修改工单回款钱
    public void updatemoney(Integer money,Integer rid){
        repairPaymentMapper.updatemoney(money,rid);
    };
    //修改工单回款状态
    public void updatestate(Integer rid){
        repairPaymentMapper.updatestate(rid);
    };
    //新增
    public void save(RepairPayment repairPayment){
        //计算回款金额如果回款等于费用就回款状态修改
        List<RepairOrder> repairOrders=repairPaymentMapper.selectgongdan((int) repairPayment.getRepairOrder().getRepairId());
        int zon=0;

        zon= (int) (repairOrders.get(0).getRepaleZmoney() + repairPayment.getReMoney());
        repairPaymentMapper.updatemoney(zon,repairPayment.getRepairOrder().getRepairId());
        if(zon==repairOrders.get(0).getRepairMoney()){
            repairPaymentMapper.updatestate(repairPayment.getRepairOrder().getRepairId());
        }
        repairPaymentDao.save(repairPayment);
    }

    //根据客户查出维修工单
    public List<RepairOrder> selecteweixu(Integer cid){
        return repairPaymentMapper.selecteweixu(cid);
    };
}
